package com.uphone.kingmall.view.indicator;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;

/**
 * @作者 radish
 * @创建日期 2019/1/17 2:57 PM
 * @邮箱 15703379121@163.com
 * @描述 默认的ViewPager适配器
 */
public class IndicatorAdapter extends BaseIndicatorAdapter {

    private final OnTabClickListener listener;
    protected String[] mItems;
    protected Context mContext;

    public IndicatorAdapter(Context context, OnTabClickListener listener, String... titles) {
        this.mItems = titles;
        this.mContext = context;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return mItems == null ? 0 : mItems.length;
    }

    @Override
    public View getView(int position, ViewGroup parent) {
        final View view = View.inflate(mContext, R.layout.kingmall_item_tab, null);
        TextView colorTrackTextView = view.findViewById(R.id.tv_tab);
//        TextView colorTrackTextView = new TextView(mContext);
        colorTrackTextView.setTextSize(getOriginTextSize());
        colorTrackTextView.setTextColor(getOriginTextColor());
        colorTrackTextView.setGravity(Gravity.CENTER);
        colorTrackTextView.setText(mItems[position]);
        return view;
    }

    @Override
    public void itemClick(View v) {
        LogUtils.e("itemClick");
        ImageView iv_tab = v.findViewById(R.id.iv_tab);
        if (v.isSelected()) {
            iv_tab.setSelected(!iv_tab.isSelected());
            if (listener != null) {
                listener.tabIvClick(iv_tab.isSelected());
            }
        }
    }

    @Override
    public void highLightIndicator(View view, int position) {
        ImageView iv_tab = view.findViewById(R.id.iv_tab);
        if (!view.isSelected()) {
            view.setSelected(true);
            TextView colorTrackTextView = view.findViewById(R.id.tv_tab);
            if (colorTrackTextView != null) {
                colorTrackTextView.setTextColor(getChangeTextColor());
                colorTrackTextView.setTextSize(getChangeTextSize());
                if (iv_tab != null) {
                    iv_tab.setImageResource(R.drawable.selector_arrow_iv);
                    iv_tab.setSelected(false);
                }
                if (listener != null) {
                    listener.tabIvClick(false);
                }
            }
        } else {

        }
    }

    @Override
    public void restoreIndicator(View view, int position) {
        view.setSelected(false);
        TextView colorTrackTextView = view.findViewById(R.id.tv_tab);
        if (colorTrackTextView != null) {
            colorTrackTextView.setTextColor(getOriginTextColor());
            colorTrackTextView.setTextSize(getOriginTextSize());
        }
        ImageView iv_tab = view.findViewById(R.id.iv_tab);
        if (iv_tab != null) {
            iv_tab.setImageResource(R.mipmap.paieu_2);
        }
    }

    @Override
    public View getBottomTrackView() {
        View view = new View(mContext);
        return view;
    }

    public interface OnTabClickListener {
        void tabIvClick(boolean selected);
    }
}
